/**
 * @param {number[]} nums
 * @return {number}
 */
 var sumOfBeauties = function(nums) {
  const n = nums.length
  const max = new Array(n)
  max[0] = nums[0] 
  for(let i = 1; i<n; i++){
    max[i] = Math.max(nums[i], max[i-1])
  }
  const min = new Array(n)
  min[n-1] = nums[n-1]
  for(let i = n-2; i>=0; i--){
    min[i] = Math.min(nums[i], min[i+1])
  }
  let ans = 0
  for(let i = 1; i<n-1; i++){
    if(max[i-1] < nums[i] && nums[i] < min[i+1]){
      ans += 2
    }else if(nums[i-1] < nums[i] && nums[i] < nums[i+1]){
      ans += 1
    }
  }
  return ans
};